<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH.'/third_party/phpsvnclient/phpsvnclient.php';
class Repository extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function browse()
	{
	    $url = 'http://code-review-tool.googlecode.com/svn';
	    $cursor = '';
	    for($i = 0; $i < func_num_args(); $i++)
	    {
	        $cursor .= func_get_arg($i). "/";
	    }
	    if ($cursor == '') {
	        $cursor = '/trunk';
	    }
	    $svnClient = new phpsvnclient($url);
	    $raw = $svnClient->getDirectoryFiles($cursor);
	    
	    $expl = explode('/', $cursor);
	    
	    $upOne = implode('/', array_slice($expl, 0, count($expl) - 2));
	    $directories = array(array('name' => '..', 'path' => $upOne, 'url' => site_url('repository/browse/'.$upOne)));
	    $files = array();
	    foreach($raw as $entry)
	    {
           $name = array_pop(explode('/', $entry['path']));
           $listing = array('name' => $name, 'path' => $entry['path'], 'url' => site_url('repository/browse/'.$entry['path']));
	       if ($entry['type'] == 'directory')
	       {
	           $name = array_pop(explode('/', $entry['path']));
	           $directories[] = $listing;
	       }
	       else if ($entry['type'] == 'file')
	       {
	           $listing['url'] = site_url('repository/view/'.$entry['path']);
               $files[] = $listing;
	       }
	    }
	    $data['title'] = 'browsing ' . $cursor;
	    $data['files'] = $files;
	    $data['directories'] = $directories;
		$this->output->set_output($this->load->view('browse', $data, true));
	}
	
	public function view()
	{
	    $this->load->library('syntaxhighlighter');
	    $url = 'http://code-review-tool.googlecode.com/svn';
	    $cursor = '';
        for($i = 0; $i < func_num_args(); $i++)
        {
            $cursor .= func_get_arg($i). "/";
        }
        if ($cursor == '') {
            $cursor = '/trunk';
        }
        $svnClient = new phpsvnclient($url);
        $file = $svnClient->getFile($cursor);
        // determine file type
        $type = pathinfo($cursor, PATHINFO_EXTENSION);
        $brush = $this->syntaxhighlighter->getBrush($type);
        
        $data = array('title' => 'File', 'file' => htmlentities($file), 'brush' => $brush);
        $this->load->view('viewer', $data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */